import axios from "axios"
import qs from "qs"

const http = axios.create({
    baseURL: process.env.NODE_ENV === "production" ? process.env.VUE_APP_BASE_API : "/api",
    timeout: 50000,
})
// 添加请求拦截器
http.interceptors.request.use(config => {
    //对发送请求之前做些什么
    return config;
}, error => {
    //对请求错误做些什么
    return Promise.reject(error)
})

//添加响应拦截器
http.interceptors.response.use(response => {
    //对响应数据做些什么
    return response
}, error => {
    //对响应错误做些什么
    return Promise.reject(error)
})

export function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        http({
            url: url,
            method: 'get',
            params: params
        }).then(res => {
            resolve(res);
        }).catch(err => {
            reject(err);
        })
    })
}

//post请求封装  默认json格式:'Content-Type':'application/json',如果是文件上传，可以修改headers为 headers: { 'Content-Type': 'multipart/form-data' }
// headers = { 'Content-Type': 'application/json' }
export function post(url, params = {}) {
    return new Promise((resolve, reject) => {
        http({
            url: url,
            method: 'post',
            data: params,
        }).then(res => {
            resolve(res);
        }).catch(err => {
            reject(err);
        })
    })
}

export default {
    get,
    post,
    http
}

